const mongoose = require('mongoose');

const resourceSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: true
  },
  type: {
    type: String,
    required: true,
    enum: ['活动', '挑战', '文章', '视频']
  },
  status: {
    type: String,
    default: 'active',
    enum: ['active', 'completed', 'cancelled']
  },
  startDate: {
    type: Date,
    default: Date.now
  },
  endDate: Date,
  // 修改 location 字段，使其更灵活
  location: {
    type: [Number],
    default: [0, 0],
    required: false // 不再是必需的
  },
  // 修改 address 字段，使其更灵活
  address: {
    type: String,
    default: '线上活动',
    required: false // 不再是必需的
  },
  checkInRequired: {
    type: Number,
    default: 0
  },
  points: {
    type: Number,
    default: 0
  },
  imageUrl: String,
  participants: [{
    userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    },
    joinedAt: {
      type: Date,
      default: Date.now
    },
    status: {
      type: String,
      default: '进行中',
      enum: ['进行中', '已完成', '已放弃']
    },
    checkins: [{
      date: {
        type: Date,
        default: Date.now
      },
      location: {
        type: [Number],
        default: [0, 0]
      },
      note: String,
      images: [String]
    }]
  }],
  createdAt: {
    type: Date,
    default: Date.now
  }
});

// 添加虚拟字段计算进度
resourceSchema.virtual('progress').get(function() {
  if (!this.checkInRequired) return 100;
  const participant = this.participants[0]; // 假设我们只关心当前用户
  return (participant?.checkins?.length || 0) / this.checkInRequired * 100;
});

module.exports = mongoose.model('Resource', resourceSchema); 